21 #define D(x) cout << #x " is " << x << endl
24 Finds the intersection between two lines (Not segments! Infinite lines)
25 Line 1 passes through points (x0, y0) and (x1, y1).
26 Line 2 passes through points (x2, y2) and (x3, y3).
28 Handles the case when the 2 lines are the same (infinite intersections),
29 parallel (no intersection) or only one intersection.
31 void line_line_intersection(double x0
, double y0
, double x1
, double y1
,
32 double x2
, double y2
, double x3
, double y3
){
37 double t0
= (y3
-y2
)*(x0
-x2
)-(x3
-x2
)*(y0
-y2
);
38 double t1
= (x1
-x0
)*(y2
-y0
)-(y1
-y0
)*(x2
-x0
);
39 double det
= (y1
-y0
)*(x3
-x2
)-(y3
-y2
)*(x1
-x0
);
42 if (fabs(t0
) < EPS
|| fabs(t1
) < EPS
){
52 double x
= x0
+ t0
*(x1
-x0
);
53 double y
= y0
+ t0
*(y1
-y0
);
54 //intersection is point (x, y)
55 printf("POINT %.2lf %.2lf\n", x
, y
);
62 printf("INTERSECTING LINES OUTPUT\n");
64 double x0
,y0
, x1
,y1
, x2
,y2
, x3
,y3
;
65 scanf("%lf %lf %lf %lf %lf %lf %lf %lf",
66 &x0
, &y0
, &x1
, &y1
, &x2
, &y2
, &x3
, &y3
);
67 line_line_intersection(x0
,y0
, x1
,y1
, x2
,y2
, x3
,y3
);
69 printf("END OF OUTPUT\n");